package com.sdy.dao.hbase;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.sdy.po.Parm;
import com.sdy.utils.DateUtil;
import com.sdy.utils.HbaseUtil;
import org.apache.hadoop.hbase.TableName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class HbaseDao {

//    @Autowired
    HbaseUtil hbaseUtil;

    //将数据插入到HBase,设计RowKey
    public Boolean insertData(JSONObject data){

        TableName tableName = TableName.valueOf("historical_data_"+data.get("companyId")+"_"+data.get("isHandled"));

        String  columFamily = JSON.parseObject(data.get("parm").toString(),new TypeReference<Parm>(){}).geteName();

        try {
            if (!hbaseUtil.getConnection().getAdmin().tableExists(tableName)){
                hbaseUtil.createTable(tableName.getNameAsString(),new String[] { "temperature", "sulfurDioxide", "nitrogenOxide","particulate","flow","humidity","oxygenContent"});
            }
            if (hbaseUtil.insertData(hbaseUtil.getTable(tableName.getNameAsString()), DateUtil.getTimeStamp(data.get("time").toString()),columFamily,"data",data.get("data").toString())){
                return true;
            }
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("插入不成功");
            return false;
        }
    }

//    public List<>

}
